package com.mk.admin.service.impl;

import com.mk.admin.mapper.RAdminRoleMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.RAdminRoleDto;
import com.mk.admin.model.entity.RAdminRoleEntity;
import com.mk.admin.model.vo.ListIdsVo;
import com.mk.admin.model.vo.RAdminRoleVo;
import com.mk.admin.service.RAdminRoleService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RAdminRoleServiceImpl implements RAdminRoleService {
    @Autowired
    private RAdminRoleMapper rAdminRoleMapper;

    @Override
    public ResultDto add(RAdminRoleVo rAdminRoleVo) {
        RAdminRoleEntity entity = JSONCopy.copy(rAdminRoleVo, RAdminRoleEntity.class);
        entity.setCreateTime(DateTool.format());
        return ResultDto.data(rAdminRoleMapper.insert(entity));
    }

    @Override
    public ResultDto update(RAdminRoleVo rAdminRoleVo) {
        RAdminRoleEntity entity = JSONCopy.copy(rAdminRoleVo, RAdminRoleEntity.class);
        return ResultDto.data(rAdminRoleMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(rAdminRoleMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(rAdminRoleMapper.status(id));
    }

    @Override
    public ResultDto addBatch(List<RAdminRoleVo> list) {
        if (list.size() == 0) {
            return ResultDto.data("error", "没有增加的角色关联");
        }
//        增加角色之前，需要删除当前用户所有的角色
        RAdminRoleEntity entity = new RAdminRoleEntity();
        entity.setUserId(list.get(0).getUserId());
        List<RAdminRoleDto> select = rAdminRoleMapper.select(entity);
        List<Integer> ids = new ArrayList<>();
        select.forEach(item -> ids.add(item.getId()));
        rAdminRoleMapper.deleteBatch(ids);
        return ResultDto.data(rAdminRoleMapper.addBatch(list));
    }

    @Override
    public ResultDto deleteBatch(List<Integer> list) {
        return ResultDto.data(rAdminRoleMapper.deleteBatch(list));
    }

}
